package com.yhsimon.udp;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

//还是要等待客户端的链接
public class UdpServerDemo2 {
    public static void main(String[] args) throws Exception {
        //开放端口
        DatagramSocket socket = new DatagramSocket(8888);

        //接收数据包
        byte[] buffer = new byte[1024];
        final DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
        socket.receive(packet);//阻塞接受
        System.out.println(packet.getAddress().getHostAddress());
        System.out.println(new String(packet.getData(),0,packet.getLength()));
        System.out.println(packet.getPort());

        //关闭资源
        socket.close();
    }
}
